#pragma once

#include "../common/decoder.h"
#include "../common/Packet.h"
#include "http_parser.h"
#include "HttpData.h"
#include <cstring>

class HttpDecoder : public ICC_Decoder
{
public:
    HttpDecoder();
	virtual ~HttpDecoder();

	virtual int ParsePacket(char *data, int len, CCSocketHandler *handle, CWspack *wspack);
    virtual void Reset() override;
    virtual bool IsComplete() const override;
    virtual std::unique_ptr<CCPacketBase> GetPacket() override;

    static bool ParserUrl(stUrlData& st_url_data, const std::string& url);

public:
    stHttpRequest m_cache;
    stHttpHead last_head_item;
    bool m_finish;

private:
	http_parser m_paeser;
    int m_offset;
};

/*
    match_flag 匹配模式
    0 向前匹配
    1 向后匹配
*/
inline bool MatchUrl(const char* method, uint32_t method_len, const char* path, uint32_t path_len, int match_flag = 0)
{
	if (path_len < method_len) return false;
    if (match_flag == 1)
    {
        int diff = path_len - method_len;
        return 0 == strncmp(method, path + diff, method_len);
    }
    else 
    {
        return 0 == strncmp(method, path, method_len);
    }
}
inline bool MatchUrlMethod(const char* method, const char* path, int match_flag = 0)
{
    return MatchUrl(method, strlen(method), path, strlen(path), match_flag);
}
















